package com.leruan.aasimp.action.insurance;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.entity.AprioriEntity;
import com.leruan.aasimp.entity.CarBedding;
import com.leruan.aasimp.entity.CarInsurance;
import com.leruan.aasimp.entity.CarInsuranceCom;
import com.leruan.aasimp.entity.CarMainInfo;
import com.leruan.aasimp.entity.CarType;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.service.CarBeddingService;
import com.leruan.aasimp.service.EauserService;
import com.leruan.aasimp.service.InsuranceComService;
import com.leruan.aasimp.service.InsuranceService;
import com.leruan.aasimp.service.CarTypeService;
import com.leruan.aasimp.util.Apriori;
import com.leruan.aasimp.util.DateUtils;
import com.leruan.aasimp.util.SecurityUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class InsuranceAction extends ActionSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 6890447963724429561L;
    private InsuranceService insuranceService;
    private InsuranceComService insuranceComService;
    private CarBeddingService carBeddingService;
    private CarTypeService carTypeService;
    private EauserService eauserService;


	public CarTypeService getCarTypeService() {
		return carTypeService;
	}

	public void setCarTypeService(CarTypeService carTypeService) {
		this.carTypeService = carTypeService;
	}

	// 首页
    public String index() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("usedCarSortList", UsedCarConstant.usedCarSortList);
        session.setAttribute("usedCarSortBean", UsedCarConstant.usedCarSortList.get(0));
        session.setAttribute("priceList", UsedCarConstant.priceList);
        session.setAttribute("carYearList", UsedCarConstant.carYearList);
        
        String mydate = new String();
		mydate +=  "<graph decimalPrecision='0' formatNumberScale='0' showLegend='0' startingAngle='180' enableSmartLabels='0' showPercentValues='1' caption='月度保险单数量占比' >";
		mydate +=  "<set name='华泰保险'  value='1";
//		mydate += object.getAsString("daymobileuc");
		mydate += "' isSliced='0'/>";
		mydate +=  "<set name='阳光保险' value='2";
//		mydate += object.getAsString("dayteluc");
		mydate += "' isSliced='0'/>";
		mydate +=  "<set name='大地财险'  value='3";
//		mydate += object.getAsString("dayunicuc");
		mydate += "'isSliced='0'/>";
		mydate +=  "<set name='平安保险'  value='3";
//		mydate += object.getAsString("dayunicuc");
		mydate += "'isSliced='0'/>";
		mydate +=  "<set name='人保财险'  value='3";
//		mydate += object.getAsString("dayunicuc");
		mydate += "'isSliced='0'/>";
		mydate +=  "<set name='太平洋保险'  value='3";
//		mydate += object.getAsString("dayunicuc");
		mydate += "'isSliced='1'/>";
		mydate += "</graph>";
		String mydate1 = new String();
		mydate1 +=  "<graph decimalPrecision='0' formatNumberScale='0' showLegend='0' startingAngle='180' enableSmartLabels='0' showPercentValues='1' caption='月度理赔单数量占比'>";
		mydate1 +=  "<set name='华泰保险'  value='12";
//		mydate1 += object.getAsString("monthmobileuc");
		mydate1 += "' isSliced='0'/>";
		mydate1 +=  "<set name='阳光保险' value='22";
//		mydate1 += object.getAsString("monthteluc");
		mydate1 += "' isSliced='0'/>";
		mydate1 +=  "<set name='大地财险' value='23";
//		mydate1 += object.getAsString("monthunicuc");
		mydate1 += "'isSliced='0'/>";
		mydate1 +=  "<set name='平安保险' value='33";
//		mydate1 += object.getAsString("monthunicuc");
		mydate1 += "'isSliced='1'/>";
		mydate1 +=  "<set name='人保财险' value='33";
//		mydate1 += object.getAsString("monthunicuc");
		mydate1 += "'isSliced='0'/>";
		mydate1 +=  "<set name='太平洋保险' value='23";
//		mydate1 += object.getAsString("monthunicuc");
		mydate1 += "'isSliced='0'/>";
		mydate1 += "</graph>";
		String mydate3 = new String();
		mydate3 +=  "<graph decimalPrecision='0' formatNumberScale='0' showLegend='0'  caption='月度同期对比' >";
		mydate3 +=  "<set name='华泰保险' value='12";
//		mydate3 += object.getAsString("periodmobileuc");
		mydate3 += "' color='AFD8F8' />";
		mydate3 +=  "<set name='阳光保险' value='2";
//		mydate3 += object.getAsString("periodteluc");
		mydate3 += "' color='F6BD0F'/>";
		mydate3 +=  "<set name='大地财险' value='6";
//		mydate3 += object.getAsString("periodunicuc");
		mydate3 += "' color='8BBA00'/>";
		mydate3 +=  "<set name='平安保险' value='5";
//		mydate3 += object.getAsString("periodunicuc");
		mydate3 += "' color='8BBA00'/>";
		mydate3 +=  "<set name='人保财险' value='-4";
//		mydate3 += object.getAsString("periodunicuc");
		mydate3 += "' color='8BBA00'/>";
		mydate3 +=  "<set name='太平洋保险' value='3";
//		mydate3 += object.getAsString("periodunicuc");
		mydate3 += "' color='8BBA00'/>";
		mydate3 += "</graph>";
		request.setAttribute("mydate", mydate);
		request.setAttribute("mydate1", mydate1);
		request.setAttribute("mydate3", mydate3);
        int pageNo = 1;
        try {
        	Map<String, String> map = new HashMap<String, String>();
        	map.put("status", "1");
        	map.put("type", "5");
            List<CarInsuranceCom> allcompany = insuranceComService.getList(map, pageNo);
            Long total = insuranceComService.getTotal(map);
            request.setAttribute("allcompany", allcompany);
            session.setAttribute("pageNoSale", pageNo);
            session.setAttribute("pageNoCenterSale", 3);
            session.setAttribute("totalNoSale", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalSale", total);
        }
        catch (Exception e) {
            e.printStackTrace();

        }
        return "show7";
    }

    public String queryCom() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        Map<String, String> map = new HashMap<String, String>();
        int pageNo = Integer.parseInt(request.getParameter("no"));
        List<CarInsuranceCom> allcompany;
        try {
        	map.put("status", "1");
        	map.put("type", "5");
            allcompany = insuranceComService.getList(map, pageNo);
            request.setAttribute("allcompany", allcompany);
            session.setAttribute("pageNoBuy", pageNo);
            Long total = (Long) session.getAttribute("totalNoBuy");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterBuy", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterBuy", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterBuy", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if(eauser==null&&eauser.getUserType()!=2){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return "index";
        }else if(eauser.getUserType()!=2){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return "index";
        }
        return "show7";
    }

    // 查询所有的历史保险套餐(自己车辆的)
    public String showall() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=2){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }

		List<CarMainInfo> carMain = eauserService.getCarManagesByUc(eauser.getUsercard());
		if(carMain==null||carMain.size()==0){
			request.setAttribute("message", "对不起，您没有车辆，不能申请保险套餐。");
			return index();
		}
		
		JSONArray car = JSONArray.fromObject(carMain);
		request.setAttribute("carMain", car.toString().replace("\"", "'"));
        
        session.setAttribute("usedCarSortList", UsedCarConstant.usedCarSortList);
        session.setAttribute("usedCarSortBean", UsedCarConstant.usedCarSortList.get(0));
        session.setAttribute("priceList", UsedCarConstant.priceList);
        session.setAttribute("carYearList", UsedCarConstant.carYearList);
        int pageNo = 1;
        try {
            Map<String, String> map = new HashMap<String, String>();
        	map.put("userid", eauser.getUserId());
            List<CarInsurance> allinsurance = insuranceService.getByList(map, pageNo);
            Long total = insuranceService.getTotal(map);
            request.setAttribute("allinsurance", allinsurance);
            session.setAttribute("pageNoBuy", pageNo);
            session.setAttribute("pageNoCenterBuy", 3);
            session.setAttribute("totalNoBuy", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalBuy", total);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "show1";
    }
    // 查询所有的历史保险套餐(自己车辆的)
    public String showall_hou() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	HttpSession session = request.getSession();
    	Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
    	List<CarInsurance> allinsurance = insuranceService.findall();
    	JSONArray array = JSONArray.fromObject(allinsurance);
		System.out.println(array.toString()); 
		request.setAttribute("allinsurance", array.toString().replace("\"", "'"));
    	return "myshow";
    }

    public String queryTc() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        Map<String, String> map = new HashMap<String, String>();
        int pageNo = Integer.parseInt(request.getParameter("no"));
        List<CarInsurance> allinsurance;
        try {
            allinsurance = insuranceService.getByList(map, pageNo);
            request.setAttribute("allinsurance", allinsurance);
            session.setAttribute("pageNoBuy", pageNo);
            Long total = (Long) session.getAttribute("totalNoBuy");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterBuy", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterBuy", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterBuy", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=2){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        return "show1";
    }

    // 查询所有的历史保险套餐(公司)
    public String showallCom() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");

        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=5){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        
        session.setAttribute("usedCarSortList", UsedCarConstant.usedCarSortList);
        session.setAttribute("usedCarSortBean", UsedCarConstant.usedCarSortList.get(0));
        session.setAttribute("priceList", UsedCarConstant.priceList);
        session.setAttribute("carYearList", UsedCarConstant.carYearList);
        int pageNo = 1;
        try {
        	Map<String,String> map = new HashMap<String, String>();
        	map.put("idCompany", eauser.getIdCompany()+"");
            List<CarInsurance> allinsurance = insuranceService.getByList(map, pageNo);
            Long total = insuranceService.getTotal(map);
            request.setAttribute("allinsurance", allinsurance);
            session.setAttribute("pageNoBuy", pageNo);
            session.setAttribute("pageNoCenterBuy", 3);
            session.setAttribute("totalNoBuy", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalBuy", total);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=5){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        return "show6";
    }

    public String queryTcCom() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        Map<String, String> map = new HashMap<String, String>();
        int pageNo = Integer.parseInt(request.getParameter("no"));
        List<CarInsurance> allinsurance;
        try {
            allinsurance = insuranceService.getByList(map, pageNo);
            request.setAttribute("allinsurance", allinsurance);
            session.setAttribute("pageNoBuy", pageNo);
            Long total = (Long) session.getAttribute("totalNoBuy");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterBuy", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterBuy", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterBuy", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=5){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        return "show6";
    }

    // 增加我的保险
    public String addTc() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date timebegin = null;
        Date timeend = null;
        try {
            timebegin = fmt.parse(request.getParameter("timebegin"));
            timeend = fmt.parse(request.getParameter("timeend"));
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        String idInsurance = request.getParameter("idInsurance");
        String idType = request.getParameter("idType");
        String idCar = request.getParameter("idCar");
        String idCompany = request.getParameter("idCompany");
        String price = request.getParameter("price");
        CarInsurance carInsurance1 =insuranceService.findBy(eauser.getUserId(),idCar);
        if(carInsurance1==null){
        	CarInsurance carInsurance = new CarInsurance();
        	carInsurance.setIdInsurance(Long.parseLong(idInsurance));
        	carInsurance.setIdType(idType);
        	carInsurance.setCreatetime(new Date());
        	carInsurance.setPrice(price);
        	carInsurance.setIdCar(idCar);
        	carInsurance.setTimebegin(timebegin);
        	carInsurance.setTimeend(timeend);
        	carInsurance.setIdCompany(Long.parseLong(idCompany));
        	carInsurance.setType(Long.parseLong("1"));
        	carInsurance.setUserid(eauser.getUserId());
        	insuranceService.addTc(carInsurance);
        	request.setAttribute("message", "申请成功！");
        }else{
        	request.setAttribute("message", "该车已经有保险了，不能再次新增！");
        }
        return showall();
    }

    // 删除套餐记录
    public String deleteTc() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        String idInsurance = request.getParameter("idInsurance");
        CarInsurance carInsurance = insuranceService.findById(Long.parseLong(idInsurance));
        insuranceService.delete(carInsurance);
        request.setAttribute("message", "删除成功！");
        return showall();
    }

    // 查询一个保险套餐
    public String showoneinsur() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        String idInsurance = request.getParameter("idInsurance");
        CarInsurance carInsurance = insuranceService.findById(Long.parseLong(idInsurance));
        request.getSession().setAttribute("carInsurance", carInsurance);
        return showall();
    }

    // 保险受理
    public String checkTc() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        String type = request.getParameter("type");
        String idInsurance = request.getParameter("ID");
        CarInsurance carInsurance = insuranceService.findById(Long.parseLong(idInsurance));
        carInsurance.setType(Long.parseLong(type));
        insuranceService.update(carInsurance);
        request.setAttribute("message", "处理成功！");
        return showallCom();
    }

    // 查询所有的历史理赔记录---（自己车辆的）
    public String showAllLp() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=2){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        
		List<CarMainInfo> carMain = eauserService.getCarManagesByUc(eauser.getUsercard());
		if(carMain==null||carMain.size()==0){
			request.setAttribute("message", "对不起，您没有车辆，不能进行理赔。");
			return index();
		}
		
		JSONArray car = JSONArray.fromObject(carMain);
		request.setAttribute("carMain", car.toString().replace("\"", "'"));
        
        session.setAttribute("usedCarSortList", UsedCarConstant.usedCarSortList);
        session.setAttribute("usedCarSortBean", UsedCarConstant.usedCarSortList.get(0));
        session.setAttribute("priceList", UsedCarConstant.priceList);
        session.setAttribute("carYearList", UsedCarConstant.carYearList);
        
        int pageNo = 1;
        try {
            Map<String, String> map = new HashMap<String, String>();
        	map.put("idUserid", eauser.getUserId());
            List<CarBedding> allcarBedding = carBeddingService.getByList(map, pageNo);
            Long total = carBeddingService.getTotal(map);
            request.setAttribute("allcarBedding", allcarBedding);
            session.setAttribute("pageNoBuy", pageNo);
            session.setAttribute("pageNoCenterBuy", 3);
            session.setAttribute("totalNoBuy", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalBuy", total);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "show2";
    }

    public String queryLp() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        Map<String, String> map = new HashMap<String, String>();
        int pageNo = Integer.parseInt(request.getParameter("no"));
        List<CarBedding> allcarBedding;
        try {
            allcarBedding = carBeddingService.getByList(map, pageNo);
            request.setAttribute("allcarBedding", allcarBedding);
            session.setAttribute("pageNoBuy", pageNo);
            Long total = (Long) session.getAttribute("totalNoBuy");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterBuy", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterBuy", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterBuy", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=2){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        return "show2";
    }

    // 查询所有的历史理赔记录---（保险公司自己的）
    public String showAllLpCom() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");

        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=5){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        
        HttpSession session = request.getSession();
        session.setAttribute("usedCarSortList", UsedCarConstant.usedCarSortList);
        session.setAttribute("usedCarSortBean", UsedCarConstant.usedCarSortList.get(0));
        session.setAttribute("priceList", UsedCarConstant.priceList);
        session.setAttribute("carYearList", UsedCarConstant.carYearList);
        int pageNo = 1;
        try {
            Map<String, String> map = new HashMap<String, String>();
        	map.put("idCompany", eauser.getIdCompany()+"");
        	
            List<CarBedding> allcarBedding = carBeddingService.getByList(map, pageNo);
            Long total = carBeddingService.getTotal(map);
            request.setAttribute("allcarBedding", allcarBedding);
            session.setAttribute("pageNoBuy", pageNo);
            session.setAttribute("pageNoCenterBuy", 3);
            session.setAttribute("totalNoBuy", total / UsedCarConstant.PAGE_SIZE + 1);
            session.setAttribute("totalBuy", total);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "show5";
    }

    public String queryLpCom() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        Map<String, String> map = new HashMap<String, String>();
        int pageNo = Integer.parseInt(request.getParameter("no"));
        List<CarBedding> allcarBedding;
        try {
            allcarBedding = carBeddingService.getByList(map, pageNo);
            request.setAttribute("allcarBedding", allcarBedding);
            session.setAttribute("pageNoBuy", pageNo);
            Long total = (Long) session.getAttribute("totalNoBuy");
            if (pageNo < 3 || total < 5) {
                session.setAttribute("pageNoCenterBuy", 3);
            }
            else if (pageNo > total - 2) {
                session.setAttribute("pageNoCenterBuy", total - 2);
            }
            else {
                session.setAttribute("pageNoCenterBuy", pageNo);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }else if(eauser.getUserType()!=5){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return index();
        }
        return "show5";
    }

    // 理赔申请
    public String addLp() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date taketime = null;
        try {
            taketime = fmt.parse(request.getParameter("taketime"));
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        String idBedding = request.getParameter("idBedding");
        String reason = request.getParameter("reason");
        String locale = request.getParameter("locale");
        String pay = request.getParameter("pay");
        String id_car = request.getParameter("id_car");
        CarBedding carBedding = new CarBedding();
        carBedding.setIdBedding(Long.parseLong(idBedding));
        carBedding.setTaketime(taketime);
        carBedding.setIdUserid(eauser.getUserId());
        carBedding.setReason(reason);
        carBedding.setLocale(locale);
        carBedding.setLosspay(pay);
        carBedding.setIdCar(id_car);
        carBedding.setType(Long.parseLong("1"));
        CarInsurance carInsurance1 =insuranceService.findBy(eauser.getUserId(),id_car);
        
        if(carInsurance1==null){
        	request.setAttribute("message", "您的车辆没有该保险套餐！");
        }else{
        	carBedding.setIdInsurance(carInsurance1.getIdInsurance());
        	carBedding.setIdCompany(carInsurance1.getIdCompany());
        	carBeddingService.addLp(carBedding);
        	request.setAttribute("message", "申请成功！");
        }
        return showAllLp();
    }

    // 删除理赔记录
    public String deleteLp() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        String idBedding = request.getParameter("idBedding");
        CarBedding carBedding = carBeddingService.findById(Long.parseLong(idBedding));
        carBeddingService.delete(carBedding);
        return showAllLp();
    }

    // 查询一个理赔
    public String showonebedding() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        String idBedding = request.getParameter("idBedding");
        CarBedding carBedding = carBeddingService.findById(Long.parseLong(idBedding));
        request.getSession().setAttribute("carBedding", carBedding);
        return showAllLp();
    }

    // 理赔受理
    public String checkLp() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        String type = request.getParameter("type");
        String idBedding = request.getParameter("ID");
        String jine = request.getParameter("jine");
        CarBedding carBedding = carBeddingService.findById(Long.parseLong(idBedding));
        carBedding.setType(Long.parseLong(type));
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
        carBedding.setAccrptname(eauser.getName());
        carBedding.setAccepttime(new Date());
        carBedding.setPay(jine);
        carBeddingService.update(carBedding);
        request.setAttribute("message", "处理成功！");
        return showAllLpCom();
    }

    // 查询所有的保险公司
    public String showAllCom() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        List<CarInsuranceCom> allcompany = insuranceComService.findall();
        request.getSession().setAttribute("allcompany", allcompany);
        return "show3";
    }
    // 查询所有的保险公司
    public String showcompany_hou() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	 Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
    	if(eauser==null){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return "companghou";
        }else if(eauser.getUserType()!=1){
        	request.setAttribute("message", "您还没有登录，请先登录！");
        	return "companghou";
        }
    	List<CarInsuranceCom> allcompany = insuranceComService.findall();
    	JSONArray array = JSONArray.fromObject(allcompany);
		System.out.println(array.toString()); 
		request.setAttribute("allinsurance", array.toString().replace("\"", "'"));
    	return "companghou";
    }
    // 查询所有的保险公司
    public String showtype_hou() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
     	if(eauser==null){
         	request.setAttribute("message", "您还没有登录，请先登录！");
         	return "typehou";
         }else if(eauser.getUserType()!=1){
         	request.setAttribute("message", "您还没有登录，请先登录！");
         	return "typehou";
         }
    	List<CarType> carType = carTypeService.findall();
    	JSONArray array = JSONArray.fromObject(carType);
		System.out.println(array.toString()); 
		request.setAttribute("carType", array.toString().replace("\"", "'"));
		
		List<CarInsuranceCom> carInsuranceCom = insuranceComService.findall();
		JSONArray array1 = JSONArray.fromObject(carInsuranceCom);
		System.out.println(array.toString()); 
		request.setAttribute("carInsuranceCom", array1.toString().replace("\"", "'"));
    	return "typehou";
    }
    // 根据保险单查询套餐名称
    public String showTcByCom() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	 String idcompany = request.getParameter("idcompany");
    	List<CarType> carType = carTypeService.findByCom(Long.parseLong(idcompany));
    	request.setAttribute("carType", carType);
    	return "showtypebycom";
    }
    // 根据保险单查询套餐名称(模态窗口)
    public String showmodelByCom() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	String idcompany = request.getParameter("idcompany");
    	List<CarType> carType = carTypeService.findByCom(Long.parseLong(idcompany));
    	request.setAttribute("carType", carType);
    	return "chosetypebycom";
    }
    // 新增险种
    public String addtype_hou() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	String idType = request.getParameter("idType");
    	String idCompany = request.getParameter("idCompany");
    	String typename = request.getParameter("typename");
    	String bedding = request.getParameter("bedding");
    	 CarType carType = new CarType();
    	 carType.setBedding(bedding);
    	 carType.setCreatetime(new Date());
    	 carType.setIdCompany(Long.parseLong(idCompany));
    	 carType.setTypename(typename);
    	 carType.setIdType(idType);
    	carTypeService.addType(carType);
    	request.setAttribute("message", "新增成功！！");
    	return showtype_hou();
    }
    // 删除type
    public String deletetype() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	String id = request.getParameter("id");
    	String idType = request.getParameter("idType");
    	CarType carType = new CarType();
    	carType.setId(Long.parseLong(id));
    	carType.setIdType(idType);
    	carTypeService.delete(carType);
    	request.setAttribute("message", "删除成功！！");
    	return showtype_hou();
    }
    // 修改type
    public String updatetype_hou() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	String idType = request.getParameter("idType");
    	String id = request.getParameter("id");
    	String createtime = request.getParameter("createtime");
    	System.out.println(createtime);
		Date createtime1 = DateUtils.string2Date(createtime);
    	String idCompany = request.getParameter("idCompany");
    	String typename = request.getParameter("typename");
    	String bedding = request.getParameter("bedding");
    	 CarType carType = new CarType();
    	 carType.setBedding(bedding);
    	 carType.setIdCompany(Long.parseLong(idCompany));
    	 carType.setTypename(typename);
    	 carType.setIdType(idType);
    	 carType.setCreatetime(createtime1);
    	 carType.setUpdatetime(new Date());
    	 carType.setId(Long.parseLong(id));
    	carTypeService.update(carType);
    	request.setAttribute("message", "修改成功！！");
    	return showtype_hou();
    }
    // 增加保险公司
    public String addcompany_hou() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	String idCompany = request.getParameter("idCompany");
    	String companyname = request.getParameter("companyname");
    	String companyphone = request.getParameter("companyphone");
    	String pecture = request.getParameter("pecture");
    	String url = request.getParameter("url");
    	CarInsuranceCom  carInsuranceCom = new CarInsuranceCom();
    	carInsuranceCom.setCompanyname(companyname);
    	carInsuranceCom.setCompanyphone(companyphone);
    	carInsuranceCom.setIdCompany(Long.parseLong(idCompany));
    	carInsuranceCom.setPecture(pecture);
    	carInsuranceCom.setUrl(url);
    	insuranceComService.save(carInsuranceCom);
    	return showcompany_hou();
    }
    // 修改保险公司
    public String updatecompany_hou() {
    	ActionContext context = ServletActionContext.getContext();
    	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    	String id = request.getParameter("id");
    	String idCompany = request.getParameter("idCompany");
    	String companyname = request.getParameter("companyname");
    	String companyphone = request.getParameter("companyphone");
    	String pecture = request.getParameter("pecture");
    	String url = request.getParameter("url");
    	CarInsuranceCom  carInsuranceCom = new CarInsuranceCom();
    	carInsuranceCom.setId(Long.parseLong(id));
    	carInsuranceCom.setCompanyname(companyname);
    	carInsuranceCom.setCompanyphone(companyphone);
    	carInsuranceCom.setIdCompany(Long.parseLong(idCompany));
    	carInsuranceCom.setPecture(pecture);
    	carInsuranceCom.setUrl(url);
    	insuranceComService.update(carInsuranceCom);
    	return showcompany_hou();
    }

    // 报表显示
    public String showBb() {
        ActionContext context = ServletActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
        Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		if(eauser==null){
			request.setAttribute("message", "您还没有登录，请先登录！");
			return index();
		}else if(eauser.getUserType()!=5){
			request.setAttribute("message", "您还没有登录，请先登录！");
			return index();
		}
        List<CarInsurance> allinsurance = insuranceService.findall();
        List<String> transSet = new ArrayList<String>();
        for (int i = 0; i < allinsurance.size(); i++) {
            String idType = allinsurance.get(i).getIdType();
            transSet.add(idType);
        }
        // 设置权系数
        Map<String, Double> map = new HashMap<String, Double>();
        
        map.put("A", 0.9);
        map.put("B", 0.8);
        map.put("C", 0.7);
        map.put("D", 0.6);
        map.put("E", 0.3);
        map.put("F", 0.4);
        map.put("G", 0.5);
        map.put("H", 0.6);
        map.put("I", 0.7);
        map.put("J", 0.1);
        String[] transSet1 = new String[transSet.size()];
        for (int j = 0; j < transSet.size(); j++) {
            transSet1[j] = transSet.get(j);
        }
        Apriori aa = new Apriori(transSet1);
        List<AprioriEntity> allapriori = aa.run(0.6, 0.2);
        for (int k = 0; k < allapriori.size(); k++) {
            String describe = allapriori.get(k).getDescribe();
            String[] split = describe.split("-->");
            List<String> list1 = new ArrayList<String>();
            for (int i = 0; i < split[0].length(); i++) {
                list1.add(split[0].substring(i, i + 1));
            }
            for (int i = 0; i < split[1].length(); i++) {
                list1.add(split[1].substring(i, i + 1));
            }
            // 计算权系数
            double s = 0;
            for (int i = 0; i < list1.size(); i++) {
                s += map.get(list1.get(i));
            }
            double weight = s / list1.size();
            allapriori.get(k).setWeightConfidence(weight * allapriori.get(k).getConfidence());
            Double count = 0.0;
            for (int i = 0; i < allinsurance.size(); i++) {
                String idType = allinsurance.get(i).getIdType();
                boolean flag = true;
                for (int j = 0; j < list1.size(); j++) {
                    if (!idType.contains(list1.get(j))) {
                        flag = false;
                    }
                }
                if (flag) {
                    count++;
                }
            }
            allapriori.get(k).setSupport(count / allinsurance.size());
            allapriori.get(k).setWeightSupport(weight * allapriori.get(k).getSupport());
        }
        request.getSession().setAttribute("allapriori", allapriori);
        return "show4";
    }

    public String updateInfo() throws UnsupportedEncodingException {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		String username = request.getParameter("username");
		String name = request.getParameter("name").trim();
		System.out.println(name);
		// name = new String(name.getBytes("iso-8859-1"),"utf-8");
		String email = request.getParameter("email");
		Long tel = Long.parseLong(request.getParameter("tel"));

		Eauser eauser = eauserService.getEauserByUn(username);
		eauser.setName(name);
		eauser.setEmail(email);
		eauser.setTel(tel);

		eauserService.updateEauser(eauser);
		request.getSession().setAttribute("eauser", eauser);
		request.setAttribute("message", "修改成功！");
		return "personInfo1";
	}
    
    public String updatePwd() throws UnsupportedEncodingException {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		String username = request.getParameter("username");
		String pwd0 = request.getParameter("pwd0");
		String pwd1 = request.getParameter("pwd1");

		Eauser eauser = eauserService.getEauserByUn(username);
		pwd0 = SecurityUtils.encodeByMD5(pwd0);
		pwd1 = SecurityUtils.encodeByMD5(pwd1);
		if (pwd0.equals(eauser.getPassword())) {
			eauser.setPassword(pwd1);
			eauserService.updateEauser(eauser);
			request.setAttribute("pwdmessage", "密码修改成功！");
		} else {
			request.setAttribute("pwdmessage", "旧密码输入有误！");
		}

		return "personInfo1";
	}
    
    public String getCompanys(){
		HttpServletRequest request = ServletActionContext.getRequest();
		List<CarInsuranceCom> companys = insuranceComService.findall();
		request.setAttribute("companys", companys);
		return "company";
	}
    public InsuranceService getInsuranceService() {
        return insuranceService;
    }

    public void setInsuranceService(InsuranceService insuranceService) {
        this.insuranceService = insuranceService;
    }

    public InsuranceComService getInsuranceComService() {
        return insuranceComService;
    }

    public void setInsuranceComService(InsuranceComService insuranceComService) {
        this.insuranceComService = insuranceComService;
    }

    public CarBeddingService getCarBeddingService() {
        return carBeddingService;
    }

    public void setCarBeddingService(CarBeddingService carBeddingService) {
        this.carBeddingService = carBeddingService;
    }

	public EauserService getEauserService() {
		return eauserService;
	}

	public void setEauserService(EauserService eauserService) {
		this.eauserService = eauserService;
	}

}
